import eventfax.core._
import java.io.IOException

class ClientReactor(starterBlock: ReactorCore => Unit) extends ReactorCore(starterBlock) {
  def check_for_actor_messages = {
    // Actor.self.receiveWithin(1) {
    //   case (msg: StringMessage, actor: Actor) => { 
    //     var t_conn = activeConnections.get(msg.binding)
    //     t_conn match {
    //       case Some(conn: Connection) => conn.asInstanceOf[SampleServer].accept_actor_response(msg.message)
    //       case None => println("Unmatched")
    //     }
    //   }
    //   case msg =>
    //     msg
    // }
  }
}

object SampleClientFactory extends ConnectionFactory {
  def create_connection(): Connection = {
    new SampleClient()
  }
}

class SampleClient extends Connection { 
  override def post_init() {

  }

  override def receive_data(p_data: FaxData) {
    send_data(p_data)
  }

  override def unbind() {
    println("Client disconnected")
  }
  
  override def connection_completed() {
    send_data("Hello World")
    // println("Connection completed")
  }

  override def on_write_error() {
    try {
      close_connection()
      println("Closing damn connection")
    } catch { case ex: IOException => println("error") }
  }
}

object FooClient {
  def main(args: Array[String]) {
    new ClientReactor(t_reactor => {
      1.to(9500).foreach(x => { 
        t_reactor.connect("localhost",9000,SampleClientFactory);
        println(x)
      })
    })
  }
}


